var car = require('../../../template/car/innercakecar.js')
var app = getApp()

Page({
  data: {
    isReady: false,
    goodscount: 0,
    indicatorDots: true,
    autoplay: false,
    dotcolor: 'rgba(0,0,0,0.3)',
    dotactivecolor: '#ffab3a',
    circular: true,
    interval: 5000,
    duration: 300,
    imgurls: []
  },
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        var wtd = res.windowWidth
        var hgt = wtd * 603 / 750
        that.setData({
          imgwtd: wtd,
          imghgt: hgt
        })
      },
    })

    if (app.globalData.tempcar != null) {
      var goodslist = app.globalData.tempcar.list1
      var goodsidlist = app.globalData.tempcar.list2
      car.init(this, goodslist, goodsidlist)
      app.globalData.tempcar = null
    }
    else {
      car.init(this)
    }
    this.setData({
      isReady: false
    })
    this.loadGoodsDetail(options.goodsid)
  },
  loadGoodsDetail: function (goodsid) {
    app.showLoading()
    var url = '/yuanmai_we/mcake/product/getProductDetail/' + goodsid
    app.get(url).then(this.loadGoodsDetailCallBack.bind(this))
  },
  loadGoodsDetailCallBack: function (res) {
    app.hideLoading()

    var goods = res.data
    var imgs = []
    var pre_url = goods.list[0].pic.url
    var pics = goods.list[0].pic.list
    for (var i = 0; i < pics.length; i++) {
      var imgurl = pre_url + pics[i].b
      imgs.push(imgurl)
    }

    var detailImgUrl = goods.details
    detailImgUrl = detailImgUrl.replace('<img src="', '')
    detailImgUrl = detailImgUrl.replace('" />', '')
    detailImgUrl = detailImgUrl.replace('"/>', '')
    var nameindex = detailImgUrl.lastIndexOf('/')
    detailImgUrl = detailImgUrl.substring(0, nameindex) + '/wap.jpg'

    var basics = goods.basic.list
    for (var i = 0; i < basics.length; i++) {
      if (basics[i].gid == 3) {
        basics[i].value = parseInt(basics[i].value)
      }
    }

    var mater = goods.mater.list
    for (var i = 0; i < mater.length; i++) {
      mater[i].img = res.data.mater.url + mater[i].img
    }

    this.setData({
      goods: goods,
      curspec: 0,
      imgurls: imgs,
      detailImgUrl: detailImgUrl,
      basic: basics,
      mater: mater,
      isReady: true
    })
    this.updateGoodsCount()
  },
  onShow: function () {
    if (app.globalData.needClearCakeCar === '1') {
      app.globalData.needClearCakeCar = null
      car.init(this)
    }
  },
  onHide: function () {
    if (app.globalData.tempcar != null) {
      return
    }
    app.globalData.tempcar = {
      list1: this.data.goodslist,
      list2: this.data.goodsidlist
    }
  },
  onUnload: function () {
    if (app.globalData.tempcar != null) {
      return
    }
    app.globalData.tempcar = {
      list1: this.data.goodslist,
      list2: this.data.goodsidlist
    }
  },
  onShareAppMessage: function () {
    return {
      title: this.data.goods.name,
      desc: this.data.goods.brief,
      path: 'pages/cake/cakedetail/cakedetail?goodsid=' + this.data.goods.id
    }
  },
  toPageTop: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 400
    })
  },
  chooseSpec: function (e) {
    var curspec = e.currentTarget.dataset.specindex
    this.setData({
      curspec: curspec
    })

    this.updateGoodsCount()
  },
  handleDetailCount: function (e) {
    var options = parseInt(e.currentTarget.dataset.options)
    var goodsinfo = this.data.goods
    var num = 1
    var specindex = this.data.curspec

    var small_img = goodsinfo.list[specindex].pic.url + goodsinfo.list[specindex].pic.list[0].s
    var nameSpec = goodsinfo.name + ' ' + goodsinfo.list[specindex].spec
    var data = {
      options: options,
      goodsid: goodsinfo.list[specindex].id,
      price: goodsinfo.list[specindex].salePrice,
      sku: goodsinfo.list[specindex].sku,
      spec: goodsinfo.list[specindex].spec,
      size: goodsinfo.list[specindex].size,
      tips: goodsinfo.list[specindex].edible,
      name: nameSpec,
      shortname: goodsinfo.name,
      img: goodsinfo.img,
      icon: goodsinfo.img,
      image: small_img,
      num: num
    }

    car.handleCount(data)
    this.updateGoodsCount()
  },
  addCarAction: function () {
    if (this.data.goodscount <= 0) {
      return
    }
    app.globalData.cake_temp_goods = {
      goods: this.data.goods,
      curspec: this.data.curspec,
      num: this.data.goodscount
    }
    wx.navigateBack()
  },
  updateGoodsCount: function () {
    var goodslist = this.data.goodslist
    var goodsidlist = this.data.goodsidlist
    var curid = this.data.goods.list[this.data.curspec].id
    var goodsCount
    if (goodsidlist.indexOf(curid) >= 0) {
      goodsCount = goodslist[goodsidlist.indexOf(curid)].num
    }
    else {
      goodsCount = 0
    }
    this.setData({
      goodscount: goodsCount
    })
  },
  showCar: car.showCar,
  submitOrder: car.submitOrder,
  closeCar: car.closeCar,
  toggleCar: car.toggleCar,
  handleCount: function (e) {
    car.handleCount(e)
    this.updateGoodsCount()
  }
})
